//new version
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

#define SIZE 6
#define FLOATNUM 6
#define MODULENUM 500

int find_id(char[], int);
int find_cycles(char[], int);
int find_times(char[], int);
int find_average(char[], int);

struct {
	int id;
	int cycle;
	int times;
	int average;
	int begin;
	int final;
}module[MODULENUM];

struct {
	int from;
	int to;
	int count;
}edge[MODULENUM];

int main()
{
	FILE *fptr;
	int i = 0;	
	char line[256];
	int semi = 0;
	int ind4mod = 0;
	int ind4edg = 1;
	int sig_edge = 0;
	int sig_set = 0;
	int time = 0;
	
	edge[0].from = -1;
	edge[0].to = 1;
	edge[0].count = 1;

	if(fptr = fopen("result", "r"))
	{
		while(fgets(line, 256, fptr))
		{
			if(line[0] == 'm')
			{
				for(i=0; i<strlen(line); i++)
				{
					if(line[i] == ':')
					{
						semi++;
						switch(semi)
						{
							case 1:
								break;
							case 2:
								module[ind4mod].id = find_id(line, i);
								break;
                                                        case 3:
                                                                module[ind4mod].cycle = find_cycles(line, i);
                                                                break;
                                                        case 4:
                                                                module[ind4mod].times = find_times(line, i);
                                                                break;
                                                        case 5:
                                                                module[ind4mod].average = find_average(line, i);
								printf("id = %d, total cycle = %d, times = %d, average = %d\n", module[ind4mod].id, module[ind4mod].cycle, module[ind4mod].times, module[ind4mod].average);
								ind4mod++;
                                                                break;
						}		
					}
				}
				semi = 0;
			}
			else if(line[0] == '<')
			{
                                for(i=0; i<strlen(line); i++)
                                {
                                        if(line[i] == ':')
                                        {	
						edge[ind4edg].count = find_cycles(line, i);//count format the same as cycle
						sig_edge = i;
						sig_set = 1;
					}
					else if(line[i] == '|')
					{
						edge[ind4edg].from = find_id(line, i);//from format the same as id
					}
					else if( i > sig_edge && line[i] == '>' && sig_set == 1)
					{
                                                edge[ind4edg].to = find_id(line, i);//from format the same as id
						printf("count = %d, %d -> %d\n", edge[ind4edg].count,  edge[ind4edg].from, edge[ind4edg].to);
						ind4edg++;
                                        }
					else
					{
					}
				}
			}
			else
			{
				printf("No info line%d!\n", i);
			}
		}
	}
	else
		printf("open err!");
	
	fclose(fptr);
	return 0;
}

int find_id(char oneline[], int ind4id)
{
	char id_temp[SIZE];
	int k = 0; // = len
        int j = 0;

        for(j=0; j<SIZE; j++)
               id_temp[j] = '\0';

	for(ind4id = ind4id+2; ind4id<strlen(oneline); ind4id++)
	{
		if(oneline[ind4id] != ' ')
		{
			id_temp[k] = oneline[ind4id];
			k++;
		}
		else
		{
			break;
		}
	}
	
	return atoi(id_temp);
}

int find_cycles(char oneline[], int ind4cyc)
{
	char cyc_temp[SIZE];
	int k = 0;
	int sig4findcyc = 0;
        int j = 0;

        for(j=0; j<SIZE; j++)
                cyc_temp[j] = '\0';

	for(ind4cyc = ind4cyc+1; ind4cyc<strlen(oneline); ind4cyc++)
        {
                if(oneline[ind4cyc] != ' ')
                {
                        cyc_temp[k] = oneline[ind4cyc];
                        k++;
			sig4findcyc = 1;
                }
                else if(oneline[ind4cyc] == ' ' && sig4findcyc == 0 )
                {
                }
		else
		{
			break;
		}
        }

	return atoi(cyc_temp);
}

int find_times(char oneline[], int ind4tim)
{
        char tim_temp[SIZE];
        int k = 0;
        int sig4findtim = 0;
	int j = 0;

	for(j=0; j<SIZE; j++)
		tim_temp[j] = '\0';
        for(ind4tim = ind4tim+1; ind4tim<strlen(oneline); ind4tim++)
        {
                if(oneline[ind4tim] != ' ')
                {
                        tim_temp[k] = oneline[ind4tim];
                        k++;
                        sig4findtim = 1;
                }
                else if(oneline[ind4tim] == ' ' && sig4findtim == 0 )
                {
                }
                else
                {
                        break;
                }
        }
        return atoi(tim_temp);
}

int find_average(char oneline[], int ind4ave)
{
        char ave_temp[SIZE];
        int k = 0; // = len
        int j = 0;
	int sig_p = 0;
	int carryin = 0;
	int temp = 0;

        for(j=0; j<SIZE; j++)
               ave_temp[j] = '\0';

        for(ind4ave = ind4ave+2; ind4ave<strlen(oneline); ind4ave++)
        {
                if(oneline[ind4ave] != ' ' && oneline[ind4ave] != '.')
                {
                        ave_temp[k] = oneline[ind4ave];
                        k++;
                }
                else if( oneline[ind4ave] == '.')
		{
			for(temp=ind4ave+1; temp<ind4ave+FLOATNUM; temp++)
			{
				if(oneline[temp] != '0')
					carryin = 1;
			}
			sig_p = 1;
		}
		else
                {
                }
		
		if(sig_p == 1)
			break;
        }
	if(carryin == 1)
	        return atoi(ave_temp)+1;
	else
		return atoi(ave_temp);
}

